<?php

namespace Xin\LaravelFortify\Validation;

use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Validation\Validator;

class ValidationServiceProvider extends ServiceProvider
{
	/**
	 * Register any application services.
	 */
	public function register(): void
	{
		$this->app->resolving(ValidationFactory::class, function (ValidationFactory $factory) {
			// 自定义验证规则
			$factory->extend('mobile', function (string $attribute, mixed $value, array $data) {
				return preg_match('/^1\d{10}+$/', $value);
			});

			// 自定义异常
			$factory->resolver(function ($translator, $data, $rules, $messages, $customAttributes) {
				$validator = new Validator($translator, $data, $rules, $messages, $customAttributes);
				$validator->setException(ValidationException::class);

				return $validator;
			});
		});
	}
}
